﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo;
using StockTrader.BusinessService.DataContract.HnxInfo;
using MySql.Data.MySqlClient;
using System.Data;
using StockTrader.DAL.MySQLHelper;

namespace StockTrader.DAL.MySQL.HnxInfo
{
    public class HnxLe : IHnxLe
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        private const string PARM_TRADINGDATE= "@TRADINGDATE";
        private const string PARM_TIME = "@TIME";
        private const string PARM_STOCKID = "@STOCKID";
        private const string PARM_PRICE = "@PRICE";
        private const string PARM_QTTY = "@QTTY";
        private const string PARM_ACCQTTY = "@ACCQTTY";
        private const string PARM_ACCVALUE = "@ACCVALUE";
        private const string PARM_TIMESTAMP = "@TIMESTAMP";
        #endregion

        #region SQL

        private const string INSERT_HNX_NEWLE = "INSERT INTO HNX_LE(TRADINGDATE, TIME, STOCKID, PRICE, QTTY, ACCQTTY, ACCVALUE, TIMESTAMP) VALUES (@TRADINGDATE, @TIME, @STOCKID, @PRICE, @QTTY, @ACCQTTY, @ACCVALUE, @TIMESTAMP) ON DUPLICATE KEY UPDATE  TIME=@TIME, STOCKID=@STOCKID, PRICE=@PRICE, QTTY=@QTTY, ACCQTTY=@ACCQTTY, ACCVALUE=@ACCVALUE, TIMESTAMP=@TIMESTAMP, TRADINGDATE=@TRADINGDATE";
        #endregion

        #region IHnxLe Members


        public List<HnxLeData> getHnxAllLe(int stockId)
        {
            throw new NotImplementedException();
        }

        public void deleteHnxLe()
        {
            throw new NotImplementedException();
        }

        public void insertHnxNewLe(HnxStockData stock)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                    new MySqlParameter( PARM_TRADINGDATE,MySqlDbType.DateTime),
                    new MySqlParameter( PARM_TIME,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_STOCKID,MySqlDbType.Int32),
                    new MySqlParameter( PARM_PRICE,MySqlDbType.Int32),
                    new MySqlParameter( PARM_QTTY,MySqlDbType.Int64),
                    new MySqlParameter( PARM_ACCQTTY,MySqlDbType.Int64),
                    new MySqlParameter( PARM_ACCVALUE,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_TIMESTAMP,MySqlDbType.Int64)                    
                };

                parms[0].Value = stock.TradingDate;
                parms[1].Value = stock.Time;
                parms[2].Value = stock.StockId;
                parms[3].Value = stock.MatchPrice;
                parms[4].Value = stock.MatchQtty;
                parms[5].Value = stock.TotalTradedQtty;
                parms[6].Value = stock.TotalTradedValue;
                parms[7].Value = stock.TimeStamp;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_HNX_NEWLE, parms);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion



    }
}
